package com.example.payment.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.alipay.api.domain.RefundGoodsDetail;
import com.alipay.api.domain.OpenApiRoyaltyDetailInfoPojo;

import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradeRefund {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();

        // 设置商户订单号
        model.setOutTradeNo("440731835782135820");



        // 设置退款金额
        model.setRefundAmount("20");

        // 设置退款请求号
        model.setOutRequestNo("HZ01RF001");


        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeRefundResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCojrnbFLWSnIBTiajU0I8awu+XhuHH14tWjomNkOsx0qhSFnwF2fivhg4ZWlMHGpdYDDaISE3xmBON3lkDxql5FE2aDa5s3w+vjaSlT/stLdBf+f2Lh1vkiBad9RVfQSI+lMZUWxxYyor0AoxLYDvB3D4cAL4rY/G0/T3YiH7B//BLp59YTFa+EOX9V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c3OebmJ/ae6vWtTwfdrhO4kw7VPDc6l93TGAWkFrPjJOW4y0wlpv1bwoUQ3fGZzKQ99waxZiwvcx+Y0m3rxMsuqPCVr3+0stF+bZAt0SisuUdAhUNqPmXQRpQvRdv51KTOGL5xKIOE+hAoGANYESIa0NRYrzx966MHga5EnQZuJuL0AUf0lom6PpOO+Ggqf2L4HRZbjPhTDcPVQgT5y7ZqBs/+FKrgQGU285wUyvafvlBiPGDRuAqpqFIw9PgIILJQTAKkix5UQ26pPtsSx/PcI+0MdckbopNk4xspYL6aE3S8Rr0k3VdPYqs0g=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu5DcUmoCK8zVeZH5gCMjEIqzCf1Az8qAXlarIcuRm9qu52b78LMnFDK5PrZpM0mofF4z07wqXDUV1Aq7wU5MBZwXesT/L90F8eoREibbICsW9aWblfPb+kTYbO3Qg58oZYwY++/ChM5BzoC43mE/rBeFOZhGcNerhLiYHeYaipgaRfohF5WAXv1C4QKct6wPzwIVLQeeN0Mpn5Y8TUK+KWJiIw0u8+nQhfgDv1IUS5cN9IXCvW41t31757/dTj/K91fCAUckLC02bIIbzsfQkhnG4srhFhpzwRJixuA7PyiDV+yxwG+Qy2il7LdM2YEEnNg0ClqNtdL3nLaCqBRwqwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000147658542");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}